package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.BlogCategory;

public class GenericBlogCategoryDAO extends AutoKeys implements
		org.jetnuke.dao.BlogCategoryDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericBlogCategoryDAO.class);

	public BlogCategory retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogCategoryModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		BlogCategory s = new BlogCategory();

		if (rs.next()) {
			fillBlogCategoryFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	protected void fillBlogCategoryFromResultSet(BlogCategory s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillBlogCategoryFromResultSet(BlogCategory, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setAID(rs.getInt("AID"));
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setID(rs.getInt(("ID")));
		s.setNAME(rs.getString("NAME"));
		s.setPARENTID(rs.getInt("PID"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillBlogCategoryFromResultSet(BlogCategory, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	public void update(BlogCategory bp) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(BlogCategory) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogCategoryModel.update"));
		p.setInt(1, bp.getAID());
		p.setString(2, bp.getNAME());
		p.setString(3, bp.getDESCRIPTION());
		p.setInt(4, bp.getPARENTID());
		p.setInt(5, bp.getID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(BlogCategory) - end"); //$NON-NLS-1$
		}
	}

	public int addNew(BlogCategory bp) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(BlogCategory) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("BlogCategoryModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("BlogCategoryModel.lastGeneratedBlogCategoriesID"));
		initBlogCategory(bp, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(BlogCategory) - end"); //$NON-NLS-1$
		}
		return id;
	}

	protected void initBlogCategory(BlogCategory bp, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initBlogCategory(BlogCategory, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setInt(1, bp.getAID());
		p.setString(2, bp.getNAME());
		p.setString(3, bp.getDESCRIPTION());
		p.setInt(4, bp.getPARENTID());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initBlogCategory(BlogCategory, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	public List selectByUser(int uid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectByUser(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogCategoryModel.selectByUser"));
		p.setInt(1, uid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectByUser(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			BlogCategory u = new BlogCategory();
			fillBlogCategoryFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public void deleteCategory(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteCategory(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogCategoryModel.delete"));
		p.setInt(1, id);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteCategory(int) - end"); //$NON-NLS-1$
		}
	}

}
